*{ 商品列表片段 }*

<ul class="product-list">
#{list items:_items, as:'commodity' }
	<li class="product">
		<div class="productInfo">
			<a target="_blank" href="@{Commodities.show(commodity.id)}">
				<div class="product-img">
					#{if commodity.images}
						#{list items:commodity.images, as:'image'}
							#{if image.sequence == 1}
							<img class="midImg" src="@{Attachments.show(image.image.id)}">
							#{/if}
						#{/list}
					#{/if}
					#{else}
					<img class="midImg" src="@{'public/images/favicon.png'}">
					#{/else}
				</div>
			</a>
				<p class="product-price"> <strong class="tmall-price">￥ ${commodity.price}</strong> </p>
			<a target="_blank" href="@{Commodities.show(commodity.id)}">
				<h3 class="product-title">${commodity.name}</h3>
			</a>
	   		<h3 class="product-sum">
	   			<strong>现货数量：</strong>
	   			#{if commodity.onSaleStatus == models.Commodity.OnSaleStatus.ONSALE_LIMITED}
					${commodity.quantityLeft} ${commodity.unit}
				#{/if}
				#{if commodity.onSaleStatus == models.Commodity.OnSaleStatus.ONSALE_UNLIMITED}
					<font class="important">长期有货</font>
				#{/if}
				#{if commodity.onSaleStatus == models.Commodity.OnSaleStatus.OFFSALE}
					<font class="important">已下架</font>
				#{/if}
	   		</h3>
	   		#{if session.username}
			<table class="product-input">
				<tr>
					<td><input class="text_1" id="quantity_${commodity.id}" type="text" maxlength="6" value="0"></td>
					<td>
						<input class="button_1" type="image" src="@{'/public/images/commodity/buy_btn.jpg'}" onclick="buy(${commodity.id});">
					</td>
					<td>
						<input class="button_1" type="image" src="@{'/public/images/commodity/addfavo_btn.jpg'}" onclick="add2Batch(${commodity.id});">
					</td>
				</tr>
			</table>
			#{/if}
		</div>
	</li>
#{/list}
</ul>
<script language="javascript">
	function buy(id) {
		var quantity = $("#quantity_"+id).val();
		$.ajax({
			url: "@{ShoppingCars.buy()}",
			data: "commodityId="+id+"&quantity="+quantity,
			type: "POST",
			dataType: "json",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				if(textStatus == "error") {
					var errJson = eval("("+XMLHttpRequest.responseText+")");
					var errMsg = "购买失败，"+errJson.message;
					$("#errMsg").html(errMsg);
					alert(errMsg);
				} else {
					var errMsg = "购买成功，请查看您的购物车。";
					alert(errMsg);
					$("#quantity_"+id).val(0);
				}
			}
		});
	}
	function add2Batch(id) {
		$.ajax({
			url: "@{Batchs.add()}",
			data: "commodityId="+id,
			type: "POST",
			dataType: "json",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				if(textStatus == "error") {
					var errJson = eval("("+XMLHttpRequest.responseText+")");
					var errMsg = "操作失败，"+errJson.message;
					$("#errMsg").html(errMsg);
					alert(errMsg);
				} else {
					window.open("@{Batchs.batch()}");
				}
			}
		});
	}
</script>